@@ -0,0 +1,53 @@ |
||
1 |
+module Agents |
|
2 |
+ class GoogleCalendarPublishAgent < Agent |
|
3 |
+ include LiquidInterpolatable |
|
4 |
+ |
|
5 |
+ cannot_be_scheduled! |
|
6 |
+ |
|
7 |
+ description <<-MD |
|
8 |
+ The GoogleCalendarPublishAgent creates events on your google calendar. |
|
9 |
+ |
|
10 |
+ |
|
11 |
+ |
|
12 |
+ Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent. |
|
13 |
+ MD |
|
14 |
+ |
|
15 |
+ def validate_options |
|
16 |
+ errors.add(:base, "expected_update_period_in_days is required") unless options['expected_update_period_in_days'].present? |
|
17 |
+ end |
|
18 |
+ |
|
19 |
+ def working? |
|
20 |
+ event_created_within?(options['expected_update_period_in_days']) && most_recent_event && most_recent_event.payload['success'] == true && !recent_error_logs? |
|
21 |
+ end |
|
22 |
+ |
|
23 |
+ def default_options |
|
24 |
+ { |
|
25 |
+ 'expected_update_period_in_days' => "10", |
|
26 |
+ 'calendar_id' => '?', |
|
27 |
+ 'message' => "{{text}}" |
|
28 |
+ } |
|
29 |
+ end |
|
30 |
+ |
|
31 |
+ def receive(incoming_events) |
|
32 |
+ incoming_events.each do |event| |
|
33 |
+ text = interpolate_string(options['message'], event.payload) |
|
34 |
+ calendar_event = publish text |
|
35 |
+ |
|
36 |
+ create_event :payload => { |
|
37 |
+ 'success' => true, |
|
38 |
+ 'published_calendar_event' => text, |
|
39 |
+ 'tweet_id' => calendar_event.id, |
|
40 |
+ 'agent_id' => event.agent_id, |
|
41 |
+ 'event_id' => event.id |
|
42 |
+ } |
|
43 |
+ end |
|
44 |
+ end |
|
45 |
+ |
|
46 |
+ def publish(text) |
|
47 |
+ calendar = GoogleCalendar.new(options, Rails.logger) |
|
48 |
+ |
|
49 |
+ calender.publish_as(options['calendar_id'], text) |
|
50 |
+ end |
|
51 |
+ end |
|
52 |
+end |
|
53 |
+ |